package com.test.service.impl;

import com.test.controller.Admininfo.MyObject;
import com.test.controller.Admininfo.ResCode;
import com.test.controller.Admininfo.ResData;
import com.test.entity.LeaderInfo;
import com.test.dao.LeaderInfoDao;
import com.test.service.LeaderInfoService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门领导(LeaderInfo)表服务实现类
 *
 * @author makejava
 * @since 2022-11-12 11:01:47
 */
@Service("leaderInfoService")
public class LeaderInfoServiceImpl implements LeaderInfoService {
    @Resource
    private LeaderInfoDao leaderInfoDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public LeaderInfo queryById(Integer id) {
        return this.leaderInfoDao.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param leaderInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Integer insert(LeaderInfo leaderInfo) {
        return this.leaderInfoDao.insert(leaderInfo);
    }

    /**
     * 修改数据
     *
     * @param leaderInfo 实例对象
     * @return 实例对象
     */
    @Override
    public LeaderInfo update(LeaderInfo leaderInfo) {
        this.leaderInfoDao.update(leaderInfo);
        return this.queryById(leaderInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.leaderInfoDao.deleteById(id) > 0;
    }

    @Override
    public ResData queryByPage(LeaderInfo leaderInfo, PageRequest pageRequest) {
        int total = Integer.parseInt(String.valueOf(this.leaderInfoDao.count(leaderInfo)));//总条数
        List<LeaderInfo> leaderInfoList = leaderInfoDao.queryAllByLimit(leaderInfo, pageRequest);//查看一共有多少条数据

        int pageSize = pageRequest.getPageSize();//每页多少条
        int countPage = (total%pageSize==0)? (total/pageSize) : ((total/pageSize)+1);//一共有多少页

        List<MyObject> list = leaderInfoList.stream().map(leaderInfo1 -> {
            return new MyObject(leaderInfo1);
        }).collect(Collectors.toList());

        return new ResData(ResCode.SUCCESS,list,total,countPage);
    }

    @Override
    public LeaderInfo selectByLeader(String name, String password) {
        return leaderInfoDao.selectByLeader(name,password);
    }

    @Override
    public String selectByName(Integer id) {
        return leaderInfoDao.selectByName(id);
    }


}
